<div class="page-breadcrumbs">
    <ul class="breadcrumb">
        <li>
            <i class="fa fa-home"></i>
            <a href="#">Home</a>
        </li>
        <li>
            <a href="#">CMS管理</a>
        </li>
        <li>
            <a href="#!cms/atcont/atcont-index.html">内容管理</a>
        </li>
        <li class="active">内容管理添加</li>
    </ul>
</div>
<!-- Page Body -->
<div class="page-body">
    <div class="row">
        <div class="col-xs-12 col-md-12">
            <div class="widget">
                <div class="widget-header bordered-bottom bordered-themeprimary">
                    <i class="widget-icon fa fa-tasks themeprimary"></i>
                    <span class="widget-caption themeprimary">内容管理基本信息</span>
                </div>
                <div class="widget-body">
                    <div class="form-horizontal bv-form" novalidate="novalidate">
                        <form id="editForm">
                            <input id="weibsite" name="weibsite" type="hidden"/>
                            <div class="form-group has-feedback">
                                <label class="col-lg-2 control-label">标题</label>
                                <div class="col-lg-9">
                                    <input type="text" class="form-control" name="title" data-bv-field="title"><i class="form-control-feedback" data-bv-field="title" style="display: none;"></i>
                                </div>
                            </div>
                            <div class="form-group has-feedback">
                                <label class="col-lg-2 control-label">简介</label>
                                <div class="col-lg-9">
                                    <input type="text" class="form-control" name="synopsis" data-bv-field="synopsis"><i class="form-control-feedback" data-bv-field="synopsis" style="display: none;"></i>
                                </div>
                            </div>
                            <div class="form-group has-feedback">
                                <label class="col-lg-2 control-label">栏目</label>
                                <div class="col-lg-9">
                                    <input type="hidden" class="form-control" id="column" name="column">
                                    <input type="text" id="columnName" class="form-control" name="columnName" data-bv-field="columnName" onclick="javascript:selectColumn();"><i class="form-control-feedback" data-bv-field="columnName" style="display: none;"></i>
                                </div>
                            </div>
                            <div class="form-group has-feedback">
                                <label class="col-lg-2 control-label">作者</label>
                                <div class="col-lg-9">
                                    <input type="text" class="form-control" name="author" data-bv-field="author"><i class="form-control-feedback" data-bv-field="author" style="display: none;"></i>
                                </div>
                            </div>
                            <div class="form-group has-feedback">
                                <label class="col-lg-2 control-label">内容</label>
                                <div class="col-lg-9">
                                    <textarea id="content" name="content" style="display:none"></textarea>
                                    <script id="editor" type="text/plain"></script>
                                </div>
                            </div>
                        </form>
                        <div class="form-group">
                            <div class="col-lg-8 col-lg-offset-4">
                                <button class="btn btn-blue" id="saveAdd">保存</button>
                                <button class="btn btn-default" id="cancel">取消</button>
                            </div>
                        </div>
                    <div/>
                </div>
            </div>
        </div>
    </div>

    <div id="columnDialog" style="display:none;">
        <div class="row" style="margin: 15px;">
            <div class="col-md-12">
                <ul id="columnTree" class="row ztree"></ul>
            </div>
        </div>
    </div>

</div>
<script type="application/javascript">
    var ue = UE.getEditor('editor',{
        initialFrameWidth :800,//设置编辑器宽度
        initialFrameHeight:250,//设置编辑器高度
        scaleEnabled:true
    });

    var selectTreeNode = null;
    var setting = {
        async: {
            enable: true,
            url:"rest/v1/cms/col/"+window.atob(GetQueryString('weibsite'))+"/childs",
            autoParam:["id=parenId"],
            dataType: "json",
            type: "get",
            dataFilter: filter
        },
        callback: {
            beforeClick: beforeClick
        }
    };

    function filter(treeId, parentNode, childNodes) {
        if (!childNodes) return null;
        for (var i=0, l=childNodes.length; i<l; i++) {
            childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
        }
        return childNodes;
    }

    function beforeClick(treeId, treeNode) {
        selectTreeNode = treeNode;
    }

    $(function(){
        $('#weibsite').val(window.atob(GetQueryString('weibsite')));
        $('#saveAdd').click(function(e){
            var data = $('#editForm').data('bootstrapValidator');
            if (data) {
                data.validate();
                if (data.isValid()) {
                    $('#content').val(ue.getContent());
                    $.ajax({
                        type : "post",
                        url : "rest/v1/cms/atcont",
                        contentType: 'application/json',
                        data: JSON.stringify($('#editForm').serializeJson()),
                        dataType: 'json',
                        success: function (response) {
                            Notify(response.message, 'top-right', '800', 'success', 'fa-bolt', true);
                            window.history.go(-1);
                        },
                        error: function(error) {
                            var obj = jQuery.parseJSON(error.responseText);
                            Notify(obj.message, 'top-right', '800', 'warning', 'fa-bolt', true);
                        }
                    });
                }
            }
        });

        $('#cancel').click(function(e){
            window.history.go(-1);
        });

        $('#editForm').bootstrapValidator({
            excluded: [':disabled'],
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                title: {
                    validators: {
                        notEmpty: {
                            message: '标题不能为空'
                        }
                    }
                },
                synopsis:{
                    validators: {
                        notEmpty: {
                            message: '简介不能为空'
                        }
                    }
                },
                columnName:{
                    validators: {
                        notEmpty: {
                            message: '栏目不能为空'
                        }
                    }
                },
                author:{
                    validators: {
                        notEmpty: {
                            message: '作者不能为空'
                        }
                    }
                }
            }
        });
    });

    function selectColumn(){
        $.fn.zTree.init($("#columnTree"), setting);
        layer.open({
            area: ['500px', '650px'],
            type: 1,
            title:'选择内容所属栏目',
            content: $('#columnDialog'),
            btn: ['选择','取消'],
            yes: function(index, layero){
                if(null != selectTreeNode){
                    $('#column').val(selectTreeNode.id);
                    $('#columnName').val(selectTreeNode.name);
                    layer.close(index);
                }else{
                    Notify('请选择栏目', 'top-right', '800', 'warning', 'fa-bolt', true);
                }
            },cancel: function(){
                layer.close(index);
            }
        });
    }
</script>
